package com.guigui.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.Query;
import com.guigui.gulimall.product.dao.SkuInfoDao;
import com.guigui.gulimall.product.entity.SkuImagesEntity;
import com.guigui.gulimall.product.entity.SkuInfoEntity;
import com.guigui.gulimall.product.entity.SpuInfoDescEntity;
import com.guigui.gulimall.product.feign.SecKillFeignService;
import com.guigui.gulimall.product.service.SkuImagesService;
import com.guigui.gulimall.product.service.SkuInfoService;
import com.guigui.gulimall.product.service.SkuSaleAttrValueService;
import com.guigui.gulimall.product.service.SpuInfoDescService;
import com.guigui.gulimall.product.to.RedisCacheSkuInfoTo;
import com.guigui.gulimall.product.vo.web.SkuItemSaleAttrVo;
import com.guigui.gulimall.product.vo.web.SkuItemVo;
import com.guigui.gulimall.product.vo.web.SpuAttrGroupVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupServiceImpl attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private SecKillFeignService secKillFeignService;

    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isNullOrEmpty(catelogId) && !"0".equals(catelogId)) {
            queryWrapper.eq("catalog_id", Long.valueOf(catelogId));
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isNullOrEmpty(brandId) && !"0".equals(brandId)) {
            queryWrapper.eq("brand_id", Long.valueOf(brandId));
        }

        String min = (String) params.get("min");
        String max = (String) params.get("max");

        if (!StringUtils.isNullOrEmpty(min) && !StringUtils.isNullOrEmpty(max)) {
            if (!"0".equals(min) || !"0".equals(max)) {
                queryWrapper.between("price",new BigDecimal(Long.parseLong(min)), new BigDecimal(Long.parseLong(max)));
            }
        }

        String key = (String) params.get("key");
        if (!StringUtils.isNullOrEmpty(key)) {
            queryWrapper.and(q -> q.eq("sku_id", key).or().like("sku_name", key));
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> selectBySpuId(Long spuId) {
        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

    @Override
    public SkuItemVo item(Long skuId) {
        SkuItemVo skuItemVo = new SkuItemVo();
        // skuInfo信息
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfo = getById(skuId);
            skuItemVo.setSkuInfo(skuInfo);
            return skuInfo;
        }, executor);

        CompletableFuture<Void> descFuture = skuInfoFuture.thenAcceptAsync((skuInfo) -> {
            // sku描述信息
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(skuInfo.getSpuId());
            skuItemVo.setDescription(spuInfoDesc);
        }, executor);


        CompletableFuture<Void> attrFuture = skuInfoFuture.thenAcceptAsync((skuInfo) -> {
            // 属性分组信息
            List<SpuAttrGroupVo> attrGroupVos = attrGroupService.getBrandWithAttrBySpuId(skuInfo.getSpuId(), skuInfo.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, executor);

        CompletableFuture<Void> saleAttrFuture = skuInfoFuture.thenAcceptAsync(skuInfo -> {
            // 销售属性信息
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(skuInfo.getSpuId());
            skuItemVo.setSaleAttrs(skuItemSaleAttrVos);
        }, executor);

        CompletableFuture<Void> imgFuture = CompletableFuture.runAsync(() -> {
            // 图片
            List<SkuImagesEntity> skuImages = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVo.setSkuImages(skuImages);
        }, executor);

        // 秒杀信息
        CompletableFuture<Void> secKillFuture = CompletableFuture.runAsync(() -> {
            RedisCacheSkuInfoTo killDetailBySkuId = secKillFeignService.getKillDetailBySkuId(skuId);
            skuItemVo.setSecKillDetail(killDetailBySkuId);
        }, executor);

        try {
            CompletableFuture.allOf(descFuture, attrFuture, saleAttrFuture, imgFuture, secKillFuture).get();
        } catch (InterruptedException | ExecutionException ignore) {
            System.err.println("item出错");
        }
        return skuItemVo;
    }

    @Override
    public List<String> getAttrStringListBySkuId(Long skuId) {
        return this.baseMapper.getAttrStringListBySkuId(skuId);
    }

}